#!/bin/bash

script_name="/opt/elasticsearch/kibana-6.3.2-linux-x86_64/bin/kibana"
pid_file="kibana.pid"

start() {
    if [ -e $pid_file ]
    then
        pid=`cat $pid_file`
	echo "Process already exists; $pid"
    else
	echo "starting $script_name"
	nohup "$script_name" 1>/dev/null 2>error.log &
	echo $! > "$pid_file"
    fi
}

stop() {
    if [ -e $pid_file ]
    then
        pid=`cat $pid_file`
        echo "killing $pid"
        kill -9 $pid
        rm $pid_file
    else
        echo "Process not started"
    fi
}

case $1 in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    status)
	if [ -e $pid_file ]; then
	    pid=`cat $pid_file`
            echo "$script_name is running with PID $pid"
        else
            echo "$script_name is not running"
        fi
        ;;
    *)  echo "usage: $0 <start|stop|restart|status>"
        exit
        ;;
esac
